package com.example.express;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.express.config.SfExpressClient;

import java.io.IOException;

public class Text {
    public static void main(String[] args) {
        // 1. 初始化客户端（参数从配置读取）
        SfExpressClient client = new SfExpressClient(
                "YPM0R2DQ",   // 替换为你的顾客编码
                "uuenezECWT8G1OF3d9YDptzbbvrazNEU",    // 替换为你的校验码
                false               // 使用沙箱环境
        );

        // 2. 构建订单JSON
        String orderId = "ORDER_" + System.currentTimeMillis();
        System.out.println(orderId);
        String msgData = SfExpressClient.buildOrderJson(
                orderId,
                "13800138000", // 寄件人电话
                "13900139000"  // 收件人电话
        );

        try {
            // 3. 调用下单接口
            String response = client.callSfApi("EXP_RECE_CREATE_ORDER", msgData);
            System.out.println("顺丰返回结果：" + response);

            // 4. 解析下单结果
            String waybillNo = parseOrderResponse(response);

            // 5. 如果下单成功，查询物流轨迹
            if (waybillNo != null) {
                System.out.println("开始查询物流轨迹...");
                String routeResponse = client.queryExpressRoute(waybillNo);
                System.out.println("物流查询结果：" + routeResponse);
                parseRouteResponse(routeResponse);
            }
        } catch (IOException e) {
            System.err.println("API调用异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    // 解析下单响应并提取运单号
    private static String parseOrderResponse(String response) {
        try {
            JSONObject result = JSON.parseObject(response);

            if ("A1000".equals(result.getString("apiResultCode"))) {
                String apiResultData = result.getString("apiResultData");
                JSONObject businessResult = JSON.parseObject(apiResultData);

                if (businessResult != null && "true".equals(businessResult.getString("success"))) {
                    JSONObject msgData = businessResult.getJSONObject("msgData");
                    if (msgData != null) {
                        JSONArray waybillNoInfoList = msgData.getJSONArray("waybillNoInfoList");
                        if (waybillNoInfoList != null && !waybillNoInfoList.isEmpty()) {
                            JSONObject waybillInfo = waybillNoInfoList.getJSONObject(0);
                            String waybillNo = waybillInfo.getString("waybillNo");
                            System.out.println("下单成功，运单号：" + waybillNo);
                            return waybillNo;
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("解析下单结果异常：" + e.getMessage());
        }
        return null;
    }

    // 解析物流查询结果
    private static void parseRouteResponse(String response) {
        try {
            JSONObject result = JSON.parseObject(response);

            if ("A1000".equals(result.getString("apiResultCode"))) {
                String apiResultData = result.getString("apiResultData");
                JSONObject businessResult = JSON.parseObject(apiResultData);

                if (businessResult != null && "true".equals(businessResult.getString("success"))) {
                    JSONObject msgData = businessResult.getJSONObject("msgData");
                    if (msgData != null) {
                        JSONArray routes = msgData.getJSONArray("routes");
                        if (routes != null && !routes.isEmpty()) {
                            System.out.println("物流轨迹：");
                            for (int i = 0; i < routes.size(); i++) {
                                JSONObject route = routes.getJSONObject(i);
                                System.out.println(route.getString("acceptTime") + " " +
                                        route.getString("acceptAddress") + " " +
                                        route.getString("remark"));
                            }
                        } else {
                            System.out.println("暂无物流轨迹信息");
                        }
                    }
                } else {
                    System.out.println("查询失败：" + (businessResult != null ? businessResult.getString("errorMsg") : "未知错误"));
                }
            }
        } catch (Exception e) {
            System.err.println("解析物流结果异常：" + e.getMessage());
        }
    }
}
